package top.wuyang.study.java24.dao;

import top.wuyang.study.java24.entity.TbCpuInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class TbCpuInfoDAO {

    private static final String INSERT = "insert into tb_cpu_info(cpu, info, cores, threads, frequency, max_frequency, post_time) values (?, ?, ?, ?, ?, ?, ?)";
    private static final String QUERY = "select * from tb_cpu_info";
    private static final String QUERY_BY_ID = "select * from tb_cpu_info where id = ?";
    private static final String QUERY_BY_CPU = "select cpu from tb_cpu_info where cpu = ?";
    private static final String DELETE = "delete from tb_cpu_info where id = ?";
    private static final String UPDATE = "update tb_cpu_info set cpu = ?, info = ?, cores = ?, threads = ?, frequency = ?, max_frequency = ?, post_time = ? where id = ?";

    public static int insert(TbCpuInfo info) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(INSERT);
        ps.setString(1, info.getCpu());
        ps.setString(2, info.getInfo());
        ps.setInt(3, info.getCores());
        ps.setInt(4, info.getThreads());
        ps.setString(5, info.getFrequency());
        ps.setString(6, info.getMax_frequency());
        ps.setTimestamp(7, new Timestamp(info.getPost_time().getTime()));
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }

    public static List<TbCpuInfo> query() throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(QUERY);
        List<TbCpuInfo> list = new ArrayList<>();
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            TbCpuInfo tbCpuInfo = new TbCpuInfo();
            tbCpuInfo.setId(rs.getInt("id"));
            tbCpuInfo.setCpu(rs.getString("cpu"));
            tbCpuInfo.setInfo(rs.getString("info"));
            tbCpuInfo.setCores(rs.getInt("cores"));
            tbCpuInfo.setThreads(rs.getInt("threads"));
            tbCpuInfo.setFrequency(rs.getString("frequency"));
            tbCpuInfo.setMax_frequency(rs.getString("max_frequency"));
            tbCpuInfo.setPost_time(rs.getTimestamp("post_time"));
            list.add(tbCpuInfo);
        }
        conn.close();
        return list;
    }

    public static TbCpuInfo queryById(Integer id) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(QUERY_BY_ID);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        TbCpuInfo info = null;
        if (rs.next()) {
            info = new TbCpuInfo();
            info.setId(rs.getInt("id"));
            info.setCpu(rs.getString("cpu"));
            info.setInfo(rs.getString("info"));
            info.setCores(rs.getInt("cores"));
            info.setThreads(rs.getInt("threads"));
            info.setFrequency(rs.getString("frequency"));
            info.setMax_frequency(rs.getString("max_frequency"));
            info.setPost_time(rs.getTimestamp("post_time"));
        }
        conn.close();
        return info;
    }

    public static TbCpuInfo queryByCpu(String cpu) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(QUERY_BY_CPU);
        TbCpuInfo info = null;
        ps.setString(1, cpu);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            info = new TbCpuInfo();
            info.setCpu(rs.getString("cpu"));
        }
        conn.close();
        return info;
    }

    public static int delete(Integer id) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(DELETE);
        ps.setInt(1, id);
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }

    public static int update(TbCpuInfo info) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(UPDATE);
        ps.setString(1, info.getCpu());
        ps.setString(2, info.getInfo());
        ps.setInt(3, info.getCores());
        ps.setInt(4, info.getThreads());
        ps.setString(5, info.getFrequency());
        ps.setString(6, info.getMax_frequency());
        ps.setTimestamp(7, new Timestamp(info.getPost_time().getTime()));
        ps.setInt(8, info.getId());
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }


}
